/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import java.io.Serializable;
import Utilities.Constants.VirtualResourceType;
import Utilities.Functions;
import Utilities.Interfaces.SelfValidating;

/**
 * This class is used for storing permissions data when writing it to or
 * retrieving it from the database.
 *
 * @author Angel Sanadinov
 */
public class PermissionsBean implements Serializable, SelfValidating
{
    private int userId;                 //id of the owner of the permissions
    private int serverId;               //id of the server on which the permissions are valid
    private String machinesPermissions; //permissions for virtual machines
    private String mediaPermissions;    //permissions for virtual media
    private String networksPermissions; //permissions for virtual networks

    /**
     * Constructs the object using the supplied data. <br><br>
     *
     * Used both when writing to the database and retrieving data from it.
     *
     * @param userId the id of the owner
     * @param serverId the id of the server on which the permissions are valid
     * @param machinesPermissions the permissions for virtual machines
     * @param mediaPermissions the permissions for virtual media
     * @param networksPermissions the permissions for virtual networks
     */
    public PermissionsBean(int userId, int serverId, String machinesPermissions, String mediaPermissions, String networksPermissions)
    {
        this.userId = userId;
        this.serverId = serverId;
        this.machinesPermissions = machinesPermissions;
        this.mediaPermissions = mediaPermissions;
        this.networksPermissions = networksPermissions;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is considered valid only when all its data is valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(Functions.isUserIdValid(userId) && Functions.isServerIdValid(serverId) 
                && Functions.arePermissionsValid(machinesPermissions)
                && Functions.arePermissionsValid(mediaPermissions)
                && Functions.arePermissionsValid(networksPermissions))
            return true;
        else
            return false;
    }

    /**
     * Retrieves the id of the user that owns these permissions.
     * 
     * @return the user id of the owner
     */
    public int getUserId()
    {
        return userId;
    }

    /**
     * Retrieves the id of the server on which these permissions are valid.
     *
     * @return the id of the server
     */
    public int getServerId()
    {
        return serverId;
    }

    /**
     * Retrieves the permissions associated with virtual machines.
     *
     * @return the machines permissions
     */
    public String getMachinesPermissions()
    {
        return machinesPermissions;
    }

    /**
     * Retrieves the permissions associated with virtual media.
     *
     * @return the media permissions
     */
    public String getMediaPermissions()
    {
        return mediaPermissions;
    }

    /**
     * Retrieves the permissions associated with virtual networks.
     *
     * @return the networks permissions
     */
    public String getNetworksPermissions()
    {
        return networksPermissions;
    }

    /**
     * Retrieves the permissions for the specified virtual resource type. <br><br>
     *
     * The results are the same as when calling the methods for the specific resource type.
     *
     * @param resourceType the type of the resource
     * @return the resource permissions or <code>null</code> when an invalid resource type
     *         is supplied
     */
    public String getResourcePermissions(VirtualResourceType resourceType)
    {
        String result = null;

        //identifies which resource permissions to return
        switch(resourceType)
        {
            case MACHINE: result = machinesPermissions; break;
            case MEDIUM: result = mediaPermissions; break;
            case NETWORK: result = networksPermissions; break;
            default: ; break;
        }

        return result;
    }

    /**
     * Builds a CSV list of the data stored in the object. <br><br>
     *
     * Format: <i>userId,serverId,machinesPermissions,mediaPermissions</i>
     *
     * @return the <code>String</code> representation of the object
     */
    @Override
    public String toString()
    {
        return userId + "," + serverId + "," + machinesPermissions + "," + mediaPermissions + "," + networksPermissions;
    }
}
